layui.config({
    base : "../../js/"
}).extend({
    "jqCommon" : "jqCommon"
})
layui.use(['form','layer','table','laytpl','jqCommon'],function(){
    var form = layui.form,
        layer = parent.layer === undefined ? layui.layer : top.layer,
        $ = layui.jquery,
        laytpl = layui.laytpl,
        table = layui.table;
        jqcommon = layui.jqCommon;

    //用户列表
    var tableIns = table.render({
        elem: '#docList',
        url : 'http://mini.yantaibaixing.cn/docs',
        method: 'GET', //如果无需自定义HTTP类型，可不加该参数
        response: {
            statusName: 'code' //数据状态的字段名称，默认：code
            ,statusCode: 200 //成功的状态码，默认：0
            ,msgName: 'message' //状态信息的字段名称，默认：msg
            ,countName: 'total' //数据总数的字段名称，默认：count
            ,dataName: 'datas' //数据列表的字段名称，默认：data
        },
        cellMinWidth : 95,
        page : false,
        height : "full-155",
        id : "docListTable",
        cols : [[
            {field: 'id', title: '协议ID', align:"center"},
            {field: 'title', title: '协议标题', align:"center"},
            {field: 'content', title: '协议内容', align:"center"},
            {title: '操作', minWidth:175, templet:'#docListBar',fixed:"right",align:"center"}
        ]]
    });

    //修改协议
    function editDoc(edit){
        var index = layui.layer.open({
            title : "修改协议",
            type : 2,
            content : "docEdit.html",
            success : function(layero, index){
                var body = layui.layer.getChildFrame('body', index);
                if(edit){
                    body.find(".id").val(edit.id);  //冗余ID
                    body.find(".title").val(edit.title);  //协议标题
                    body.find(".content").val(edit.content);  //协议内容
                    form.render();
                }
                setTimeout(function(){
                    layui.layer.tips('点击此处返回协议列表', '.layui-layer-setwin .layui-layer-close', {
                        tips: 3
                    });
                },500)
            }
        })
        layui.layer.full(index);
        window.sessionStorage.setItem("index",index);
        //改变窗口大小时，重置弹窗的宽高，防止超出可视区域（如F12调出debug的操作）
        $(window).on("resize",function(){
            layui.layer.full(window.sessionStorage.getItem("index"));
        })
    }

    //列表操作
    table.on('tool(docList)', function(obj){
        var layEvent = obj.event,
            data = obj.data;

        if(layEvent === 'edit'){ //编辑
            editDoc(data);
        }else if(layEvent === 'usable'){ //启用禁用
            var _this = $(this),
                usableText = "是否确定禁用此用户？",
                btnText = "已禁用";
            if(_this.text()=="已禁用"){
                usableText = "是否确定启用此用户？",
                btnText = "已启用";
            }
            layer.confirm(usableText,{
                icon: 3,
                title:'系统提示',
                cancel : function(index){
                    layer.close(index);
                }
            },function(index){
                _this.text(btnText);
                layer.close(index);
            },function(index){
                layer.close(index);
            });
        }else if(layEvent === 'del'){ //删除
            layer.confirm('确定删除此用户？',{icon:3, title:'提示信息'},function(index){
                // $.get("删除文章接口",{
                //     newsId : data.newsId  //将需要删除的newsId作为参数传入
                // },function(data){
                    tableIns.reload();
                    layer.close(index);
                // })
            });
        }
    });
})
